package com.tzstcl.sys.user.ctrl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tzstcl.base.ctrl.BaseCtrl;
import com.tzstcl.base.model.AjaxResult;
import com.tzstcl.commons.utils.StringUtils;
import com.tzstcl.framework.constants.AppConstants;
import com.tzstcl.sys.user.model.SysPermission;
import com.tzstcl.sys.user.service.SysPermissionService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.List;

/**
 * 公司：天筑科技股份有限公司
 * 作者：zgq
 * 日期：2019年05月09日
 * 说明：系统权限表Controller
 */
@Controller
@RequestMapping("/admin/sysPermission")
public class SysPermissionCtrl extends BaseCtrl {

    @Autowired
    private SysPermissionService sysPermissionService;

    /**
     * 新增 修改
     * @param sysPermission
     * @return
     */
    @RequestMapping("/save")
    @RequiresPermissions(value={"sysPermission:edite","sysPermission:add"},logical=Logical.OR)
    @ResponseBody
    public AjaxResult save(@Valid SysPermission sysPermission) {
         return toAjax(sysPermissionService.insertOrUpdate(sysPermission));
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sysPermission:delete")
    @ResponseBody
    public AjaxResult delete(String ids) {
        return toAjax(sysPermissionService.deleteBatchIds(ids));
    }

    /**
     * 获取单条信息
     * @param id
     * @return
     */
    @RequestMapping("/get")
    @RequiresPermissions("sysPermission:view")
    @ResponseBody
    public AjaxResult get(Long id) {
        return  success("获取信息成功",sysPermissionService.getOnePermission(id));
    }

    /**
     * 获取查询的分页数据
     * @param sysPermission
     * @return
     */
    @RequestMapping("/list")
    @RequiresPermissions("sysPermission:view")
    @ResponseBody
    public List<SysPermission> list(SysPermission sysPermission) {
        return  sysPermissionService.selectList(sysPermission);
    }

    /**
     * list页面导航
     * @return
     */
    @RequiresPermissions("sysPermission:view")
    @RequestMapping("/toList")
    public String toList() {
        return "admin/sys/user/sysPermissionList";
    }

    /**
     * form页面导航
     * @return
     */
    @RequiresPermissions(value={"sysPermission:edite","sysPermission:add"},logical=Logical.OR)
    @RequestMapping("/toForm")
    public String toForm(@RequestParam(value="id", required=false)  String id, Model model) {
        if(StringUtils.isNotBlank(id)){
        model.addAttribute("id",id);
        }
        return "admin/sys/user/sysPermissionForm";
    }

    /**
     * 菜单数据
     *
     * @return
     */
    @RequestMapping(value = "/zTreeNodes")
    @ResponseBody
    public AjaxResult getZTreeNodes() {

        JSONArray array = new JSONArray();
        JSONObject object = null;
        List<SysPermission> list = sysPermissionService.selectList(null);
        for (SysPermission permission : list) {
            object = new JSONObject();
            object.put("id", ""+permission.getId());
            object.put("pId", ""+permission.getParentId());
            object.put("name", permission.getName());
            array.add(object);
        }
        return success(AppConstants.SUCCESS_MSG, array);
    }
}